package com.guigu.www.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guigu.www.pojo.*;
import com.guigu.www.service.ApplyService;
import com.guigu.www.service.FileService;
import com.guigu.www.service.imp.IdUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class ApplyController {
    // 产品生产计划
    @Autowired
    ApplyService applyService;

    @Autowired
    FileService fileService;

    @Autowired
    IdUtils idUtils;

    //查询所有
    @RequestMapping("queryApplylist.action")
    public PageVo<Apply> queryApplylist(Apply apply,
                                        @RequestParam(value = "pagenum", defaultValue = "1") int pagenum,
                                        @RequestParam(value = "pagesize", defaultValue = "5") int pagesize) {
        //条件查询
        QueryWrapper<Apply> queryWrapper = new QueryWrapper<Apply>();
        queryWrapper.eq("check_tag","S001-1");

        Page<Apply> page = applyService.page(new Page<Apply>(pagenum, pagesize), queryWrapper);
        PageVo<Apply> pageVo = new PageVo<Apply>();
        pageVo.setRows(page.getRecords());
        pageVo.setTotal(page.getTotal());
        return pageVo;
    }



    //查询要审核
    @RequestMapping("queryApplylistshtg.action")
    public PageVo<Apply> queryApplylistshtg(Apply apply,
                                            @RequestParam(value = "pagenum", defaultValue = "1") int pagenum,
                                            @RequestParam(value = "pagesize", defaultValue = "5") int pagesize) {
        //条件查询
        QueryWrapper<Apply> queryWrapper = new QueryWrapper<Apply>();
        queryWrapper.eq("CHECK_TAG", "S001-0");
        queryWrapper.eq("MANUFACTURE_TAG", "P001-0");
        Page<Apply> page = applyService.page(new Page<Apply>(pagenum, pagesize), queryWrapper);
        PageVo<Apply> pageVo = new PageVo<Apply>();
        pageVo.setRows(page.getRecords());
        pageVo.setTotal(page.getTotal());
        return pageVo;
    }

    //查询tg
    @RequestMapping("queryApplylistall.action")
    public PageVo<Apply> queryApplylistall(Apply apply,
                                            @RequestParam(value = "pagenum", defaultValue = "1") int pagenum,
                                            @RequestParam(value = "pagesize", defaultValue = "5") int pagesize) {
        //条件查询
        QueryWrapper<Apply> queryWrapper = new QueryWrapper<Apply>();
        Page<Apply> page = applyService.page(new Page<Apply>(pagenum, pagesize), queryWrapper);
        PageVo<Apply> pageVo = new PageVo<Apply>();
        pageVo.setRows(page.getRecords());
        pageVo.setTotal(page.getTotal());
        return pageVo;
    }


    //产品表添加到 apply
    @RequestMapping("adddesignapplyshow.action")
    public Map adddesignapplyshow(@RequestBody Apply apply
    ) {
        Map map = new HashMap();
        System.out.println(apply);
        apply.setApplyid(idUtils.getid3(apply));
        apply.setChecktag("S001-1");
        apply.setManufacturetag("P001-0");
        apply.setRegister(apply.getRegister());
        apply.setRegistertime(apply.getRegistertime());
        apply.setRemark(apply.getRemark());
        apply.setDesigner(apply.getRegister());
        List<Apply> list = apply.getDetailsss();
        for (Apply details : list) {
            apply.setType("Y001-1");
            apply.setProductid(details.getProductid());
            apply.setId(null);
            apply.setAmount(details.getAmount());
            apply.setProductname(details.getProductname());
            apply.setProductdescribe(details.getProductdescribe());
            apply.setRegister(details.getRegister());
        }
       boolean f = applyService.save(apply);
        if (f) {
            map.put("msg", "添加成功,请复核");
        } else {
            map.put("msg", "添加失败");
        }
        return map;
    }



    //修改不通
    //readdapplying
    @RequestMapping("readdapplying.action")
    public Map addapplying(@RequestBody Apply apply
    ) {
        boolean f = false;
        apply.setApplyid(idUtils.getid3(apply));
        apply.setChecker("S001-2");
        apply.setManufacturetag("P001-0");
        apply.setApplyid(idUtils.getid3(apply));
        Map map = new HashMap();
        List<File> list = apply.getDetailss();
        for (File details : list) {
            apply.setType(details.getType());
            apply.setProductid(details.getProductid());
            apply.setId(null);
            apply.setProductname(details.getProductname());
            apply.setProductdescribe(details.getProductdescribe());
            apply.setRegistertime(details.getRegistertime());
            apply.setRegister(details.getRegister());
            f = applyService.save(apply);
            if (f) {
                f = true;
                break;
            }
        }
        if (f) {
            map.put("msg", "复核不通过，请重新制定");
        } else {
            map.put("msg", "失败");
        }

        return map;
    }


    //修改通
    @RequestMapping("updatescglloginshow.action")
    public Map updateapplypojo(@RequestBody Apply apply
    ) {
        System.out.println(apply);
        Map map = new HashMap();
        apply.setChecktag("S001-0");
        boolean f = applyService.updateById(apply);
        if (f) {
            map.put("msg", "审核成功");
        } else {
            map.put("msg", "审核失败");
        }
        return map;
    }


    //(序号)
    @GetMapping("queryapplybyid.action/{id}")
    public Apply queryapplybyid(@PathVariable Integer id) {
        return applyService.getById(id);
    }


    //更据applyid查询apply
    @RequestMapping("queryapplyzj.action/{applyid}")
    public PageVo<Apply> queryapplyzj(Apply apply,
                                      @PathVariable String applyid,
                                      @RequestParam(value = "pagenum", defaultValue = "1") int pagenum,
                                      @RequestParam(value = "pagesize", defaultValue = "5") int pagesize) {
        QueryWrapper<Apply> queryWrapper = new QueryWrapper<Apply>();
        Apply d = applyService.getById(applyid);

        queryWrapper.eq("APPLY_ID", d.getApplyid());

        Page<Apply> page = applyService.page(new Page<Apply>(pagenum, pagesize), queryWrapper);

        PageVo<Apply> pageVo = new PageVo<Apply>();
        pageVo.setRows(page.getRecords());
        pageVo.setTotal(page.getTotal());

        return pageVo;
    }


}
